from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField

# Create your models here.
from mlh.utils.models import BaseModel


class Spit(BaseModel):
    """
    吐槽
    """
    user = models.ForeignKey("users.User", on_delete=models.CASCADE,
                             related_name="spits", verbose_name="用户")
    content = RichTextUploadingField(max_length=140, verbose_name="内容")
    comment_counts = models.IntegerField(verbose_name="评论量", default=0)
    share_counts = models.IntegerField(verbose_name="分享", default=0)
    is_collection = models.BooleanField(verbose_name="是否收藏", default=False)
    like_counts = models.IntegerField(verbose_name="点赞量", default=0)

    class Meta:
        db_table = "tb_spits"
        verbose_name = "吐槽"
        verbose_name_plural = verbose_name
        ordering = ['-create_time']  # 指明默认排序

    def __str__(self):
        return "%s : %s" % (self.user_id, self.content)


class SpitComment(BaseModel):
    """评论"""
    user = models.ForeignKey("users.User", on_delete=models.CASCADE,
                             related_name="spit_comments", verbose_name="用户")
    spit = models.ForeignKey(Spit, on_delete=models.CASCADE,
                             related_name="spit_comments",
                             verbose_name="评论")
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True,
                               blank=True, max_length=140,
                               related_name="sub_comments",
                               verbose_name="父级")
    like_counts = models.IntegerField(default=0, verbose_name="评论点赞量")
    content = models.CharField(max_length=140, verbose_name="内容", default=None)

    class Meta:
        db_table = "tb_spit_comments"
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s - %s : %s" % (self.user_id, self.spit_id, self.parent_id)


class SpitCollection(BaseModel):
    """收藏"""
    spit = models.ForeignKey(Spit, on_delete=models.CASCADE, verbose_name="收藏的吐槽")
    user = models.ForeignKey("users.User", on_delete=models.CASCADE,
                             related_name="spit_collections",
                             verbose_name="用户")

    class Meta:
        db_table = "tb_spit_collections"
        verbose_name = "收藏"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s - %s : %s" % (self.user_id, self.spit_id, self.spit.content)


class SpitLike(BaseModel):
    """吐槽点赞"""
    spit = models.ForeignKey(Spit, on_delete=models.CASCADE, verbose_name="点赞的吐槽")
    user = models.ForeignKey("users.User", on_delete=models.CASCADE,
                             related_name="spit_likes",
                             verbose_name="用户")
    is_like = models.BooleanField(default=False, verbose_name="是否点赞")

    class Meta:
        db_table = "tb_spit_likes"
        verbose_name = "吐槽点赞"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s : %s" % (self.user_id, self.spit_id)


class SpitCommentLike(BaseModel):
    """评论点赞"""
    comment = models.ForeignKey(SpitComment, on_delete=models.CASCADE,
                                null=False, verbose_name="点赞的评论")
    spit = models.ForeignKey(Spit, on_delete=models.CASCADE,
                             verbose_name="点赞的评论的吐槽")
    user = models.ForeignKey("users.User", on_delete=models.CASCADE,
                             related_name="spit_comment_likes",
                             verbose_name="用户")
    # is_like = models.BooleanField(default=False, verbose_name="是否点赞")

    class Meta:
        db_table = "tb_spit_comment_likes"
        verbose_name = "评论点赞"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s - %s" % (self.user_id, self.spit_id)


